package com.gxa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.common.R;
import com.gxa.entity.DistrictSysOrganization;
import com.gxa.entity.DistrictSysUser;
import com.gxa.mapper.DistrictOrganizationMapper;
import com.gxa.service.DistrictOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DistrictOrganizationServiceImpl implements DistrictOrganizationService {

    @Autowired
    private DistrictOrganizationMapper districtOrganizationMapper;


    /**
     * 添加组织信息
     * @param districtSysOrganization
     * @return
     */
    @Override
    @Transactional
    public R addOrganization(DistrictSysOrganization districtSysOrganization) {
        R r = new R();
        try {
            //先判断要添加的组织是否已经存在
            //查询条件（最上层组织、父级编号、组织名称）
            Map<String,Object> conditionMap = new HashMap<>();
            conditionMap.put("topOrganization",districtSysOrganization.getTopOrganization());
            conditionMap.put("parentId",districtSysOrganization.getParentId());
            conditionMap.put("orgName",districtSysOrganization.getOrgName());

            DistrictSysOrganization selectedDistrictSysOrganization = districtOrganizationMapper.selectOrganizationByCondition(conditionMap);
            if(selectedDistrictSysOrganization == null){
                //添加的组织不存在，进行添加
                districtOrganizationMapper.insertOrganization(districtSysOrganization);
                r.setCode(200);
                r.setMsg("添加成功！");
            }else {
                r.setCode(1001);
                r.setMsg("添加失败，要添加的组织已存在！");
            }
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,添加失败,请联系管理员");
        }
        return r;
    }

    /**
     * 分页查询对应区级的组织信息
     * @param currentPage
     * @param pageSize
     * @param topOrganization
     * @return
     */
    @Override
    public R queryAllOrganizationsByPage(int currentPage, int pageSize, String topOrganization) {
        R r = new R();
        try{
            Page<DistrictSysOrganization> page = PageHelper.startPage(currentPage,pageSize);
            List<DistrictSysOrganization> districtSysOrganizationList = districtOrganizationMapper.selectAllOrganizations(topOrganization);
            int count = (int)page.getTotal();
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(districtSysOrganizationList);
            r.setCount(count);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,查询失败,请联系管理员");
        }
        return r;
    }

    /**
     * 修改组织信息
     * @param districtSysOrganization
     * @return
     */
    @Override
    @Transactional
    public R updateOrganization(DistrictSysOrganization districtSysOrganization) {
        R r = new R();
        try {
            //先根据组织编号删除对应的组织信息
            districtOrganizationMapper.deleteOrganizationById(districtSysOrganization.getOrgId());
            //然后添加组织信息
            districtOrganizationMapper.insertOrganizationWithFixedId(districtSysOrganization);
            r.setCode(200);
            r.setMsg("修改成功！");
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,修改失败,请联系管理员");
        }
        return r;
    }

    /**
     * 根据组织编号删除组织信息
     * @param id
     * @return
     */
    @Override
    @Transactional
    public R deleteOrganizationById(int id) {
        R r = new R();
        try {
            //首先根据组织编号查询组织信息
            List<DistrictSysUser> districtSysUserList = districtOrganizationMapper.selectDistrictSysUsersById(id);
            if(districtSysUserList.size() == 0){
                int row = districtOrganizationMapper.deleteOrganizationById(id);
                if(row > 0){
                    r.setCode(200);
                    r.setMsg("删除成功！");
                }else {
                    r.setCode(500);
                    r.setMsg("删除失败！");
                }
            }else {
                r.setCount(1002);
                r.setMsg("该组织关联了若干数据，不支持删除！");
            }
        }catch (Exception e){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            r.setCode(500);
            r.setMsg("系统异常,删除失败,请联系管理员");
        }
        return r;
    }

    /**
     * 查询对应区级的所有组织信息
     * @param topOrganization
     * @return
     */
    @Override
    public R queryAllOrganizations(String topOrganization) {
        R r = new R();
        try{
            List<DistrictSysOrganization> districtSysOrganizationList = districtOrganizationMapper.selectAllOrganizations(topOrganization);
            r.setCode(200);
            r.setMsg("查询成功！");
            r.setData(districtSysOrganizationList);
        }catch (Exception e){
            e.printStackTrace();
            r.setCode(500);
            r.setMsg("系统异常,查询失败,请联系管理员");
        }
        return r;
    }
}
